﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "GbmpUiPlatformCommandIds.h"
#include "DrawArcByCenterEndPtElementBehavior.h"
#include "AddGbmpDynamicPannelUtils.h"
#include "GbmpControlIds.h"
#include "IUserTransaction.h"
#include "IModelView.h"
#include "IArc3d.h"
#include "IModelLine.h"
#include "IGraphicsElementShape.h"
#include "UiDocumentViewUtils.h"
#include "GcmpActionBase.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd)

GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd()
    : CmdCreateArcByCenterEndPtElement(ID_CMD_GBMP_DRAW_ARC_CENTER_MODEL_LINE_WITHOUT_REF)
{
}

GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::~GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd()
{
}

bool GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::OnExecuteCommand()
{
    return true;
}

void GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::SetupRibbonContextualPanel()
{
    AddGbmpDynamicPannelUtils::SetupModelLineCreationContextWithoutRef(m_pDrawArcByCenterEndPtAction, ID_PAGE_GBMP_MODIFY);
}

OwnerPtr<DrawArcByCenterEndPtElementBehavior> GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::CreateDrawArcByCenterEndPtElementBehavior()
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");


    OwnerPtr<DrawArcByCenterEndPtElementBehavior> opBehavior = NEW_AS_OWNER_PTR(DrawArcByCenterEndPtElementBehavior, pDoc);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opBehavior, L"opGridNewBehavior为空",L"GDMPLab",L"2024-03-30");
    opBehavior->SetOnFinishCallback(DrawArcByCenterEndPtElementBehavior::CreateOnFinishCallback(&GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::OnArcFinish, this));

    return TransferOwnership(opBehavior);
}

bool GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::CreatePresetElement(ElementId& elementId)
{
    elementId = ElementId::InvalidID;
    return false;
}

bool GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::OnArcFinish(IUiView* pCurrentView, const Vector3d& centerPt, const Vector3d& startPt, const Vector3d& endPt)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制圆心-起点-终点圆弧模型线"));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opTransaction.get(), L"opTransaction为空",L"GDMPLab",L"2024-03-30");

    // 将三维坐标点转化为平面上的二维点
    Vector3d startVec = startPt - centerPt;
    double radius = startVec.Normalize();
    Vector3d endVec = endPt - centerPt;
    endVec.Normalize();
    Vector3d realEndPt = centerPt + endVec * radius;
    //起点终点连线的中垂线
    Vector3d tmpDir = (endPt + startPt)*0.5 - centerPt;
    tmpDir.Normalize();
    Vector3d onArcPt = centerPt + tmpDir*radius;
    OwnerPtr<IArc3d> opCurve = IArc3d::Create(startPt, realEndPt, onArcPt);
    IModelLine* pModelLine = IModelLine::Create(pDoc, TransferOwnership(opCurve), ElementCreationOptions::Normal);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelLine, L"创建ModelLine失败",L"GDMPLab",L"2024-03-30");
    
    opTransaction->Commit();

    return true;
}

bool GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::OnRadiusUpdate(IUiView* pCurrentView, const Vector3d& firstPt, const Vector3d& pos,
    OwnerPtr<IGraphicsElementShape>& opGElementShape)
{
    return false;
}

bool GbmpDrawArcByCenterEndPtModelLineWithoutRefCmd::OnArcUpdate(IUiView* pCurrentView, const Vector3d& centerPt, const Vector3d& startPt, const Vector3d& pos,
    OwnerPtr<IGraphicsElementShape>& opGElementShape)
{
    return false;
}
